/*
 * @Author: your name
 * @Date: 2021-02-28 13:37:13
 * @LastEditTime: 2021-04-28 08:46:57
 * @LastEditors: Please set LastEditors
 * @Description: 菜单目录管理
 * @FilePath: \jianfa-api\routes\admin\menu.js
 */
// 菜单目录管理
const express = require("express");
const router = express.Router();
const Result = require("../../models/result");
const upload = require("../../utils/multer.js");
const {
    addMenuService,
    addMenuFileService,
    getMenuService,
    getMenuFileService,
    delMenuFileService,
    delMenuService,
    updateMenuService,
} = require("../../service/menuService");
/**
 * @description: 新增三级子目录
 * @param {cid} 父亲id
 * @param {type} img or pdf
 *  @param {class_name} 目录名字
 * @return {*}
 */
router.post("/add_menu", async (req, res, next) => {
    const { status } = await addMenuService(req.body);
    if (status) {
        new Result({}, "插入成功").success(res);
    } else {
        new Result({}, "获取失败").fail(res);
    }
});
// 目录文件上传
router.post("/add_file", upload.array("photos"), async (req, res, next) => {
    let filesInfo = []; //文件信息数组
    req.files.forEach((file) => {
        let obj = {};
        obj.name = file.filename;
        obj.filePath = `public/img/${obj.name}`;
        obj.path = file.path;
        filesInfo.push(obj);
    });
    try {
        const { status } = await addMenuFileService(filesInfo, req.body.sid, req.body.table_name);
        if (status) {
            const data = {
                filesInfo,
                count: filesInfo.length,
            };
            new Result(data, "上传成功").success(res);
        }
    } catch ({ err }) {
        new Result(err, "上传失败").fail(res);
    }
});
//获取三级菜单列表
router.post("/get_menu", async (req, res, next) => {
    try {
        const { status, results } = await getMenuService(req.body);
        if (status) {
            new Result(results, "获取成功").success(res);
        }
    } catch ({ err }) {
        new Result(err, "获取失败").fail(res);
    }
});
// 修改三级菜单
router.post("/update_menu", async (req, res, next) => {
    try {
        const { status } = await updateMenuService(req.body);
        if (status) {
            new Result({}, "修改成功").success(res);
        }
    } catch ({ err }) {
        new Result(err, "修改失败").fail(res);
    }
});
//获取三级菜单文件列表
router.post("/get_menu_flies", async (req, res, next) => {
    try {
        const { status, results } = await getMenuFileService(req.body);

        if (status) {
            let data = { results, total: global.all_let.TOTAL };
            global.all_let.TOTAL != null ? null : delete data.total;
            new Result(data, "查询成功").success(res);
        }
    } catch ({ err }) {
        new Result(err, "获取失败").fail(res);
    }
});

//根据id删除文件
router.post("/delete_file", async (req, res, next) => {
    const { id } = req.body;
    const ress = await delMenuFileService(req.body);
    if (ress) {
        new Result({ id }, "删除成功").success(res);
    } else {
        new Result({}, "删除失败").fail(res);
    }
});

//根据id删除三级菜单
router.post("/delete_menu", async (req, res, next) => {
    const { id } = req.body;
    const ress = await delMenuService(req.body);
    if (ress) {
        new Result({ id }, "删除成功").success(res);
    } else {
        new Result({}, "删除失败").fail(res);
    }
});

module.exports = router;
